#include "StdAfx.h"
#include "Floor.h"
#include "Passenger.h"

CFloor::CFloor(size_t floorNum) : m_floorNum(floorNum), m_elevatorArrivedListener(NULL), m_waitCount(0)
{
}

CFloor::~CFloor(void)
{
	if (m_elevatorArrivedListener != NULL)
		delete m_elevatorArrivedListener;

	if (m_callRequestlistener != NULL)
		delete m_callRequestlistener;

	for (PassengerCollection::iterator it = m_incomingPassengers.begin(); it != m_incomingPassengers.end(); ++it)
		delete *it;
}

void CFloor::ElevatorArrived()
{
	if (m_elevatorArrivedListener != NULL)
		m_elevatorArrivedListener->LoadPassengersAtArival(m_incomingPassengers);
	
	m_waitCount = 0;
}

void CFloor::ElevatorLeft()
{
	if (m_callRequestlistener != NULL && !m_incomingPassengers.empty())
		m_callRequestlistener->OnCallRequest(m_floorNum, m_incomingPassengers[0]->NeedUp());
}

void CFloor::RegisterListeners(ILoadPassengersListener* loadPassengerslistener, ICallRequestListener* callRequestListener)
{
	m_elevatorArrivedListener = loadPassengerslistener;
	m_callRequestlistener = callRequestListener;
}

void CFloor::AddIncomingPassengers(const std::vector<size_t>& passengerTargets)
{
	m_incomingPassengers.clear();
	m_incomingPassengers.reserve(passengerTargets.size());
	for (size_t i=0; i<passengerTargets.size(); ++i)
	{
		CPassenger* pas = new CPassenger(GetNum(), passengerTargets[i]);
		m_incomingPassengers.push_back(pas);
	}
}